package com.sunrun.sunrunframwork.uibase;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.sunrun.sunrunframwork.R;
import com.sunrun.sunrunframwork.http.BaseBean;
import com.sunrun.sunrunframwork.http.NetServer;
import com.sunrun.sunrunframwork.http.cache.NetSession;
import com.sunrun.sunrunframwork.http.intf.UIUpdateHandler;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 对话框Fragment基类
 * Created by WQ on 2017/4/26.
 */

public abstract  class BaseDialogFragment extends DialogFragment implements UIUpdateHandler {
    protected FragmentActivity _CONTEXT;
    Unbinder unbinder;
    NetServer netServer;
    public NetServer getNetRequest() {
        if(netServer==null){
            netServer=new NetServer(getActivity(),this);
        }
        return netServer;
    }
    protected abstract int getLayoutId();
    @Override
    public abstract void onViewCreated(View view, @Nullable Bundle savedInstanceState);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        _CONTEXT = getActivity();
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        unbinder= ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void nofityUpdate(int requestCode, BaseBean bean) {

    }

    @Override
    public void nofityUpdate(int requestCode, float progress) {

    }

    @Override
    public void dealData(int requestCode, BaseBean bean) {

    }

    @Override
    public void loadFaild(int requestCode, BaseBean bean) {

    }

    @Override
    public void emptyData(int requestCode, BaseBean bean) {

    }

    @Override
    public void requestFinish() {

    }

    @Override
    public void requestCancel() {

    }

    @Override
    public void requestStart() {

    }

    public NetSession getSession() {
        return NetSession.instance(_CONTEXT);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        _CONTEXT = null;
    }

    public void finish() {
        if (_CONTEXT != null)
            _CONTEXT.finish();
    }
    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return createDefaultDialog();
    }

    public Dialog createDefaultDialog(){
            Dialog dialog = new Dialog(getActivity(), R.style.NoTitleDialog);
            Window dialogWindow = dialog.getWindow();
           // dialogWindow.setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_VISIBLE | SOFT_INPUT_ADJUST_RESIZE);
            //设置对话框从底部进入
//            dialogWindow.setWindowAnimations(R.style.bottomInWindowAnim);
            WindowManager.LayoutParams p = dialogWindow.getAttributes();
            p.width = ViewGroup.LayoutParams.MATCH_PARENT;
            p.height = ViewGroup.LayoutParams.MATCH_PARENT;//高度自己设定
            dialogWindow.setAttributes(p);
            dialogWindow.setGravity(Gravity.CENTER);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            return dialog;
    }
}
